function! s:airline() abort
    if exists('g:loaded_airline') && g:loaded_airline
        let g:airline_statusline_ontop = 1
        " let g:airline_left_sep = '▶'
        " let g:airline_left_alt_sep = '❯'
        " let g:airline_right_sep = '◀'
        " let g:airline_right_alt_sep = '❮'
        " let g:airline_symbols.dirty= '⚡'       
        if !exists('g:airline_symbols')
            " let g:airline_symbols = {}
            " let g:airline_symbols.linenr = '¶'
            " let g:airline_symbols.branch = '⎇'
            " let g:airline_symbols.readonly = ''
            " let g:airline_symbols.maxlinenr = ''
        endif
        call s:extensions()
    endif
endfunction

function! s:capslock() abort
    let g:airline#extensions#capslock#enabled = 1
    let g:airline#extensions#capslock#symbol = 'CAPS'
endfunction

function! s:coc() abort
    let g:airline#extensions#coc#enabled = 1
endfunction

function! s:vimtex() abort
    let g:airline#extensions#vimtex#enabled = 1
    let g:airline#extensions#vimtex#left = "{"
    let g:airline#extensions#vimtex#right = "}"
    let g:airline#extensions#vimtex#main = ""
    let g:airline#extensions#vimtex#sub_main = "m"
    let g:airline#extensions#vimtex#sub_local = "l"
    let g:airline#extensions#vimtex#compiled = "c₁"
    let g:airline#extensions#vimtex#continuous = "c"
    let g:airline#extensions#vimtex#viewer = "v"
    let g:airline#extensions#vimtex#wordcount = 1
endfunction

function! s:ale() abort
    " ale
    let g:airline#extensions#ale#enabled = 1
    " bookarmks
    let g:airline#extensions#bookmark#enabled = 1
endfunction

function! s:branch() abort
    " branch
    let g:airline#extensions#branch#enabled = 1
    let g:airline#extensions#branch#empty_message = ''
    let g:airline#extensions#branch#vcs_priority = ["git", "mercurial"]
    let g:airline#extensions#branch#use_vcscommand = 1
    let g:airline#extensions#branch#format = 0
    let g:airline#extensions#branch#sha1_len = 10
    let g:airline#extensions#branch#vcs_checks = ['untracked', 'dirty']
    let g:airline#extensions#branch#displayed_head_limit = 10
endfunction

function! s:bufferline() abort
    " bufferline
    let g:airline#extensions#bufferline#enabled = 1
    let g:airline#extensions#bufferline#overwrite_variables = 1
    " call api#bufferline#init()
endfunction

function! s:tabline() abort
    call api#tabline#init()
endfunction

function! s:ctrlp() abort
    let g:airline#extensions#ctrlp#show_adjacent_modes = 1
    let g:airline#extensions#ctrlp#color_template = 'insert' 
    let g:airline#extensions#ctrlp#color_template = 'normal'
    let g:airline#extensions#ctrlp#color_template = 'visual'
    let g:airline#extensions#ctrlp#color_template = 'replace'
endfunction 

function! s:extensions() abort
    let g:airline_extensions = ['default', 'branch', 'tabline', 'bufferline', 'coc', 'ctrlp']
    call s:ale()
    call s:branch()
    call s:bufferline()
    " CapsLock
    call s:capslock()
    " Coc
    call s:coc()
    " vimtex
    call s:vimtex()
    " call s:tabline()
endfunction

function! api#airline#init() abort
    if !exists('g:loaded_airline')
        try
            packadd vim-airline
            packadd vim-airline-themes
        catch /.*/
            echoerr v:exception
        endtry
    endif
    if exists('g:loaded_airline')
        " call s:airline()
        let g:toggle_keymap.a = [":AirlineToggle", "airline"]
    endif
endfunction
